﻿#define _CRT_SECURE_NO_WARNINGS  1
//一定要动手，例子不管多么简单，建议至少自己手敲一遍看看是否理解了里头的细枝末节。
//一定要学会思考，思考为什么要这样，而不是那样。还要举一反三地思考
#include"Link_List.h"
SListNode* BuySListNode(SLTDateType x)
{
	SListNode* node = (SListNode*)malloc(sizeof(SListNode));
	if (node == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	node->data = x;
	node->next = NULL;
	return node;
}
void SListPrint(SListNode* plist)
{
	assert(plist);
	while (plist->next != NULL)
	{
		printf("%d ", plist->data);
		plist = plist->next;
	}
	printf("%d ",plist->data);

	//SListNode* cur = plist;
	//while (cur)
	//	//while (cur != NULL)
	//{
	//	printf("%d->", cur->data);
	//	cur = cur->next;
	//}
	//printf("NULL\n");
}
void SListPushBack(SListNode** pplist, SLTDateType x)
{
	assert(*pplist);
	SListNode* node = BuySListNode(x);
	if ((*pplist) == NULL)
	{
		*pplist = node;
	}
	else {
		SListNode* tail = *pplist;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = node;
		node->next = NULL;
	}

}
void SListPushFront(SListNode** pplist, SLTDateType x)
{
	assert(*pplist);
	SListNode* node = BuySListNode(x);
	if ((*pplist) == NULL)
	{
		*pplist = node;
	}
	else
	{
		node->next = (*pplist);
		(*pplist) = node;
	}


	assert(pplist);

	//// 1.空
	//// 2.非空
	//SListNode* newnode = BuySListNode(x);
	//if (*pplist == NULL)
	//{
	//	*pplist = newnode;
	//}
	//else
	//{
	//	newnode->next = *pplist;
	//	*pplist = newnode;
	//}

}
void SListPopBack(SListNode** pplist)
{
	assert(pplist);
	assert(*pplist);
	SListNode* tail = *pplist;
	if (tail->next == NULL||tail == NULL)
	{
		return;
	}
	while (tail->next->next != NULL)
	{
		tail = tail->next;
	}

	tail->next = NULL;

	//SListNode* prev = NULL;
	//SListNode* tail = *pplist;
	//// 1.空、只有一个节点
	//// 2.两个及以上的节点
	//if (tail == NULL || tail->next == NULL)
	//{
	//	free(tail);
	//	*pplist = NULL;
	//}
	//else
	//{
	//	while (tail->next)
	//	{
	//		prev = tail;
	//		tail = tail->next;
	//	}

	//	free(tail);
	//	tail = NULL;

	//	prev->next = NULL;
	//}


}
void SListPopFront(SListNode** pplist)
{
	assert(pplist);
	assert(*pplist);
	if ((*pplist)->next == NULL)
	{
		return;
	}
	*pplist = (*pplist)->next;


	//// 1.空
	//// 2.一个
	//// 3.两个及以上
	//SListNode* first = *pplist;
	//if (first == NULL)
	//{
	//	return;
	//}
	//else if (first->next == NULL)
	//{
	//	free(first);
	//	*pplist = NULL;
	//}
	//else
	//{
	//	SListNode* next = first->next;
	//	free(first);
	//	*pplist = next;
	//}

}
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
	assert(plist);
	while (plist->data != x)
	{
		plist = plist->next;
		if (plist == NULL)
		{
			break;
		}
	}
	if (plist == NULL)
	{
		return NULL;
	}
	return plist;



	/*SListNode* cur = plist;
	while (cur)
	{
		if (cur->data == x)
			return cur;

		cur = cur->next;
	}

	return NULL;*/
}
void SListInsertAfter(SListNode* pos, SLTDateType x)
{
	assert(pos);
	SListNode* node = BuySListNode(x);
	node->next = pos->next;
	pos->next = node;

	//assert(pos);
	//SListNode* next = pos->next;
	//// pos newnode next
	//SListNode* newnode = BuySListNode(x);
	//pos->next = newnode;
	//newnode->next = next;
}
void SListEraseAfter(SListNode* pos)
{
	assert(pos);
	//pos->next = pos->next->next;
	SListNode* next = pos->next;
	if (next != NULL)
	{
		SListNode* nextnext = next->next;
		free(next);
		pos->next = nextnext;
	}


	//assert(pos);
	//// pos next nextnext
	//SListNode* next = pos->next;

	//if (next != NULL)
	//{
	//	SListNode* nextnext = next->next;
	//	free(next);
	//	pos->next = nextnext;
	//}
}
void SListDestroy(SListNode* plist)
{
	assert(plist);
	SListNode* node = NULL;
	while (plist->next != NULL)
	{
		node = plist->next;
		free(plist);
		plist = node;
	}
	free(plist);
}
